// pages/bill/billDetail/billDetail.js
const app = getApp();
var date = require('../../util/date.js');
var util = require('../../util/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    distance: "",
    time: 0,
    leadHour: 0,
    leadDay: 0,
    detail: {},
    tocalc: {},
    isOpenId: true,
    nickName: null,
    avatarUrl: null,
    isFast: false,
  },
  //快速导航的点击
  tapIn(e) {
    let index = e.currentTarget.dataset.index;
    if (index == "1") {
      //收起
      this.setData({
        isFast: false
      })
    } else {
      //展开
      this.setData({
        isFast: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let openId = (wx.getStorageSync('openId'));
    if (!openId){
      //未登录 不展示
      this.setData({
        isOpenId: false
      })
      return
    }
    if(!options.id){
      wx.showToast({
        title: '当前网络异常，请稍后重试',
        icon: "none",
        duration: 2000
      })
      return
    }
    //用户昵称和头像前台自获取
    let user = app.globalData.userInfo
    //console.info(user);
    //设置数据
    this.setData({
      id: options.id,
      distance: options.distance,
      time: options.time, //时间以分为单位,小于60分直接展示
      nickName: user.nickName,
      avatarUrl: user.avatarUrl,
    })
    //处理时间
    var time = parseInt(options.time);
    if (time < (24 * 60)) {//小于24小时,显示小时
      this.setData({
        leadHour: parseInt(time / 60)
      })
    } else {//其余都显示天数
      this.setData({
        leadDay: parseInt(time / 60 / 24)
      })
    }
    this.getDetail(options.id);
  },
  //获取详情
  getDetail (id) {
    const that = this;
    const url = app.globalData.api + "/wechat/bill/get/"+id;
    wx.request({
      url: url,
      method: "get",
      success: function (res) {
        if(res.data.errCode == 0){
          if (!res.data.data.remarks){
            res.data.data.remarks = "暂无备注"
          }
          let item = res.data.data
          //处理到期日期
          item.oDate = date.formatDate(item.expiringDate, 'yyyy-MM-dd')
          let mRate = util.dot(parseFloat(item.interestRate) / 100 / 12 * 1000);
          //console.info(item);
          that.setData({
            detail: item,
            tocalc: {
              billType: item.billType,
              currentDate: new Date(),
              expiringDate: item.expiringDate,
              changeDays: 0,
              days: item.diffDay,//剩余天数
              billAmount: item.billAmount,//金额
              yearRate: util.dot(item.interestRate),//年利率
              monthRate: mRate,//月利率
              serviceCharge: util.dot(item.serviceCharge),//手续费
              discountInterest: 0,
              discountAmount: 0,
              debit10: 0,
              isBillDetail: true,
              isCalcDetail: false
            }
          })
        }else{
          wx.showToast({
            title: '请求失败，请稍后重试',
            icon: "none",
            duration: 2000
          })
        }
      },
      fail:function(e){
        console.info(e);
      }
    })
  },
  //点击我要发布，跳转发布页面
  toBillRelease() {
    wx.navigateTo({
      url: '../billRelease/billRelease'
    })
  },
  //点击计算，跳转计算器
  toCalc() {
    var data1 = this.data;
    //传参 ： 剩余天数，到期日期 （贴现日期是当天）金额 年利率 手续费 每十万扣款 
    app.globalData.calcParameters = data1.tocalc;
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  //点击电话，跳出弹层
  calling: function (e) {
    var mobile = e.currentTarget.dataset.mobile;
    var username = e.currentTarget.dataset.username;
    wx.showModal({
      title: '提示',
      confirmText: "是",
      confirmColor: "#004884",
      cancelText: "否",
      content: '是否拨打' + username + '电话：' + mobile,
      success: (res) => {
        if (res.confirm) {
          //点击确认
          wx.makePhoneCall({
            phoneNumber: mobile, //此号码并非真实电话号码，仅用于测试  
            success: function (suc) {
              console.log(suc)
            },
            fail: function (err) {
              wx.showToast({
                title: err.errMsg,
                icon: "none"
              })
            }
          })
        }
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})